import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_taotaoshipin/pages/root_tab/root_tab_controller.dart';
import 'package:flutter_taotaoshipin/pages/root_tab/root_tab_state.dart';
import 'package:get/get.dart';

class RootTabPage extends StatefulWidget {
  const RootTabPage({super.key});

  @override
  State<RootTabPage> createState() => _RootTabPageState();
}

class _RootTabPageState extends State<RootTabPage> {
  final RootTabController controller = Get.find();
  final RootTabState state = Get.find<RootTabController>().state;

  @override
  void initState() {
    super.initState();

    // 是 StatefulWidget 渲染结束的回调，只会被调用一次，之后 StatefulWidget 需要刷新 UI 也不会被调用，
    // 这个方法在一帧的最后调用，并且只调用一次,使用这个方法就可以在判断渲染完成，并获取到元素的大小。
    WidgetsBinding.instance.addPostFrameCallback((_) {
      // 显示状态栏和导航栏
      SystemChrome.setEnabledSystemUIMode(
        SystemUiMode.manual,
        overlays: [
          SystemUiOverlay.top,
          SystemUiOverlay.bottom,
        ],
      );
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        physics: const NeverScrollableScrollPhysics(),
        controller: state.pageController,
        onPageChanged: (idx) {},
        children: state.pageList,
      ),
      bottomNavigationBar: Obx(
        () => CupertinoTabBar(
          items: state.itemList,
          currentIndex: state.selectedIndex.value,
          onTap: controller.switchTap,
        ),
      ),
    );
  }
}
